package com.zjht.gmcc.servlet.provider;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyPath;

public class EditProvider extends BaseServlet {

	private static final long serialVersionUID = 2205937410942000119L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {
		int i_flag = 0;
		PreparedStatement ps, ps1;
		ResultSet rs;

		String privodeid = request.getParameter("pid");
		
		String provideName = request.getParameter("pName");// 供应商名称
		String provideType = request.getParameter("qyxz");// 供应商类型
		String legalPerson = request.getParameter("pfrdb");// 法人pfrdb
		String firmage =  request.getParameter("pDate");// 成立时间
		String regcapital = request.getParameter("zczb");// 注册资本
		String sslb = request.getParameter("sslb");// 所属类别
		String rangetxt = request.getParameter("rangetxt");// 经营范围
		String reglicence = request.getParameter("reglicence");// 营业执照
		String IDpath = request.getParameter("IDpath");// 联系人身份证
		String legalPersonID = request.getParameter("legalPersonID");// 法人身份证
		String orgCode = request.getParameter("orgCode");// 组织编码
		String taxPath = request.getParameter("taxpath");// 纳税
		String proCode=request.getParameter("pCode"); 
		//
		String address =request.getParameter("address"); 
		String posCode = request.getParameter("posCode"); 
		String website = request.getParameter("website");
		String linkName = request.getParameter("linkName");
		String IDnum =  request.getParameter("IDnum");
		String s_pictype = null; 
		String fax = request.getParameter("fax");
		String phone =  request.getParameter("phone");
		String email =  request.getParameter("email");
		String firstName = "";
		int server =Integer.parseInt(request.getParameter("serverid"));
		
		try {
			
		  
			String s_sql = "";


			
			
				
				
				
				
				s_sql="delete from providerdetail where providerID=?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, privodeid); 
				i_flag = ps.executeUpdate();
				if (i_flag != 0) {//拆解服务能力表
					s_sql = "INSERT INTO PROVIDERDETAIL (PROVIDERID,BRAND,PRODUSTTYPE,SALEZONE,VALIDDATE,FILEPATH,QUANTITY) " + "VALUES (?,?,?,?,?,?,?)";
					ps = myJDBC.prepareStatement(s_sql);
					ps.setString(1, privodeid);
					for (int i = 0; i < server+1; i++) {
						if(request.getParameter("ppara_add["+i +"]brand")!=null){
							ps.setString(2, request.getParameter("ppara_add["+i +"]brand"));
							ps.setString(3, request.getParameter("ppara_add["+i +"]protype"));
							ps.setString(4, request.getParameter("ppara_add["+i +"]area"));
							ps.setString(5, request.getParameter("ppara_add["+i +"]vDate"));
							ps.setString(6, request.getParameter("ppara_add["+i +"]file"));
							ps.setString(7, request.getParameter("ppara_add["+i +"]pjxs"));
							i_flag = ps.executeUpdate();
						}
						
					}
					
				} 
			
			if (i_flag != 0) {
				s_sql = "UPDATE  PROVIDER SET PROVIDERCODE=?,PROVIDERNAME=?,LEGALPERSON=?,PROVIDERTYPE=?,FIRMAGE=?," +
						"	REGCAPITAL=?,TRADEID=?,BUSINESS=?,ADDRESS=?,HOMEPAGE=?,LINKMAN=?,IDCARD=?,"
						+ "FAX=?,LINKPHONE=?,EMAIL=?,LEGALPERSONID=?,ORGCODE=?,IDFILEPATH=?,REGLICENCE=?,REGTAX=?"
						+ "WHERE PROVIDERID=? ";
				ps = myJDBC.prepareStatement(s_sql);
				
				ps.setString(1, proCode);
				ps.setString(2, provideName);
				ps.setString(3, legalPerson);
				ps.setString(4, provideType);
				ps.setString(5, firmage);
				ps.setString(6, regcapital);
				ps.setString(7, sslb);
				ps.setString(8, rangetxt);
				ps.setString(9, address);
				ps.setString(10, website);
				ps.setString(11, linkName);
				ps.setString(12, IDnum);
				ps.setString(13, fax);
				ps.setString(14, phone);
				ps.setString(15, email);
				ps.setString(16, legalPersonID);
				ps.setString(17, orgCode);
				ps.setString(18, IDpath);
				ps.setString(19, reglicence);
				ps.setString(20, taxPath);
				ps.setString(21, privodeid);
				i_flag = ps.executeUpdate();
			}

			if (i_flag == 1) {
				alertAndReturnValue(out, "修改供应商信息成功!", "1");
			} else {
				errAlert(request, out, menu, "修改失败");
			}
			return i_flag == 1;

		} catch (Exception e) {
			throw e;
		} finally {

			
		}

	}

}
